package com.xkk.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.xkk.po.Tmemory;

public class SoapMemory extends BaseSoapCon {

	public SoapMemory(String webm) {
		super(webm);
		// TODO Auto-generated constructor stub
	}
	public void addmemory(Tmemory memory)
	{
		wsdlUrl = "http://192.168.13.95:8080/webwork/services/Service";
		webMethod = "memoryAdd";
		namespace = "http://service.xkk.com";
		soapAction = namespace + webMethod;
		soapObject = new SoapObject(namespace, webMethod);
		soapObject.addProperty("mem",memory);
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);	
		envelope.setOutputSoapObject(soapObject);
		envelope.addMapping("http://po.xkk.com/xsd","Tmemory", memory.getClass());
		HttpTransportSE se = new HttpTransportSE(wsdlUrl);
		try {
			se.call(soapAction,envelope);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public List<Map<String,Object>> memoryGetall(int id,int type)
	{
		wsdlUrl = "http://192.168.13.95:8080/webwork/services/Service";
		webMethod = "memoryGetall";
		namespace = "http://service.xkk.com";
		soapAction = namespace + webMethod;
		soapObject = new SoapObject(namespace, webMethod);
		List<Map<String,Object>> li=new ArrayList<Map<String,Object>>();
		Tmemory mem=new Tmemory();
		setproperity(new String[]{"id","type"},new Object[]{id,type});
		envelope.bodyOut = soapObject;
		HttpTransportSE se = new HttpTransportSE(wsdlUrl);
		try {
			se.call(soapAction, envelope);
			if(envelope.getResponse()!=null)
			{
				
				Object vect =  envelope.getResponse();
				Vector<SoapObject> vec=(Vector<SoapObject>) vect;
				for(int i=0;i<vec.size();i++)
				{
					SoapObject so=vec.get(i);
					Map<String,Object> map=new HashMap<String, Object>();
					map.put("dateid", so.getProperty(0));
					Log.i("0",so.getProperty(0).toString());
					Log.i("1",so.getProperty(1).toString());
					Log.i("2",so.getProperty(2).toString());
					Log.i("3",so.getProperty(3).toString());
					Log.i("4",so.getProperty(4).toString());
					Log.i("5",so.getProperty(5).toString());
					Log.i("6",so.getProperty(6).toString());
					Log.i("7",so.getProperty(7).toString());
					Log.i("8",so.getProperty(8).toString());
					map.put("datefrom", so.getProperty("datefrom"));
					map.put("datebody", so.getProperty("datebody"));
					map.put("publishtime", so.getProperty("publishtime"));
					
					float res=Float.parseFloat( so.getProperty("averagescore").toString());
					map.put("averagescore", res);
					map.put("viewtype", so.getProperty("viewtype"));
					byte[] b=Base64.decode(so.getProperty("viewpic").toString());
					Bitmap bmap=BitmapFactory.decodeByteArray(b, 0, b.length);
					map.put("viewpic", bmap);
					map.put("locationname", so.getProperty("locationname"));
					map.put("commentnumber", so.getProperty("commentnumber"));
					li.add(map);
				}
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return li;
		
	}

}
